import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
/**
 * 前序遍历节点并使用 callback 进行转换
 */
var map = function map(treeNodes, callback, options) {
  var _ref = options !== null && options !== void 0 ? options : {},
    _ref$level = _ref.level,
    level = _ref$level === void 0 ? 0 : _ref$level,
    _ref$childrenKey = _ref.childrenKey,
    childrenKey = _ref$childrenKey === void 0 ? 'children' : _ref$childrenKey,
    parent = _ref.parent;
  return treeNodes.map(function (treeNode, index) {
    var children = treeNode[childrenKey];
    if (children !== null && children !== void 0 && children.length) {
      var treeNodeWithOutChildren = {};
      Object.keys(treeNode).forEach(function (key) {
        // @ts-ignore
        if (key !== childrenKey) treeNodeWithOutChildren[key] = treeNode[key];
      });
      var convertedTreeNode = callback(treeNodeWithOutChildren, {
        level: level,
        index: index,
        parent: parent,
        isMaybeLeaf: false
      });
      return _objectSpread(_objectSpread({}, convertedTreeNode), {}, _defineProperty({}, childrenKey, map(children, callback, {
        childrenKey: childrenKey,
        level: level + 1,
        parent: treeNode
      })));
    }
    return callback(treeNode, {
      level: level,
      index: index,
      parent: parent,
      isMaybeLeaf: true
    });
  });
};
export default map;